import { storeToRefs } from 'pinia';
import { useCommonApi } from '/@/api/common';
import { useCityList } from '/@/stores/cityList';
import { useSourceList } from '/@/stores/sourceList';
import { useSite } from '/@/stores/siteList';

const storesCity = useCityList();
const { storeCityList } = storeToRefs(storesCity);

const storesSource = useSourceList();
const { storeSourceList } = storeToRefs(storesSource);

const storesSite = useSite();
const { storeSiteList } = storeToRefs(storesSite);

const { getSiteList, getCityList } = useCommonApi();

// // 全局的获取站点列表的方法
export function getCommonSiteList() {
	return new Promise(async (resolve) => {
		let list = storeSiteList.value;
		if (list.length !== 0) {
			resolve(list);
			return;
		}
		try {
			let res = await getSiteList();
			if (res.code === 200) {
				let siteList = [];
				siteList = res.data;
				storesSite.setSiteList(siteList);
				resolve(siteList);
			}
		} catch (error) {
			console.log('获取全局的站点error', error);
		}
	});
}

// // 全局的站点匹配货币单位的方法
export function getCurrencyUnit(shorthand) {
	let list = storeSiteList.value;
	let currencyName = '';
	for (let index = 0; index < list.length; index++) {
		const element = list[index];
		if (element.shorthand === shorthand) {
			currencyName = element.currency;
		}
	}
	return currencyName + '';
}

// // 全局的站点匹配中文名称的方法
export function getSiteNameByShorthand(shorthand) {
	let list = storeSiteList.value;
	let name = '';
	for (let index = 0; index < list.length; index++) {
		const element = list[index];
		if (element.shorthand === shorthand) {
			name = element.name;
		}
	}
	return name + '';
}

// 全局的获取国家列表的方法
export function getCommonCityList() {
	return new Promise(async (resolve) => {
		try {
			if (storeCityList.value.length !== 0) {
				resolve(storeCityList.value);
				return;
			}
			let res = await getCityList();
			if (res.code === 200) {
				let cityList = [];
				cityList = res.data;
				storesCity.setCityList(cityList);
				resolve(cityList);
			}
		} catch (error) {
			console.log('获取全局的国家error', error);
		}
	});
}

// 全局的匹配国家id对应名字的方法
export function getCityNameByShorthand(shorthand) {
	let list = storeCityList.value;
	let name = '';
	for (let index = 0; index < list.length; index++) {
		const element = list[index];
		if (element.shorthand === shorthand) {
			name = element.countriesRegions;
		}
	}
	return name + '';
}

// 全局的获取来源列表的方法
export function getCommonSourceList() {
	return new Promise(async (resolve) => {
		if (storeSourceList.value.length !== 0) {
			resolve(storeSourceList.value);
			return;
		}
		try {
			let res = await useCommonApi().getSourceList();
			if (res.code === 200) {
				let list = [];
				list = res.data;
				storesSource.setSourceList(list);
				resolve(list);
			}
		} catch (error) {
			console.log('获取全局的来源error', error);
		}
	});
}

// 全局的获取文件的格式
export function checkFormatByFileName(fileName) {
	try {
		let index = fileName.lastIndexOf('.'); //（考虑严谨用lastIndexOf(".")得到）得到"."在第几位
		let fileNameSuffix = fileName.substring(index); //截断"."之前的，得到后缀
		if (/(.*)\.(mp4|avi|wmv|MP4|AVI|WMV)$/.test(fileNameSuffix)) {
			//根据后缀，判断是否符合视频格式
			return 'video';
		} else if (/(.*)\.(jpg|JPG|bmp|BMP|mpg|MPG|mpeg|MPEG|tis|TIS|webp|WEBP|gif|GIF)$/.test(fileNameSuffix)) {
			//根据后缀，判断是否符合图片格式
			return 'image';
		} else if (/(.*)\.(pdf|PDF)$/.test(fileNameSuffix)) {
			//根据后缀，判断是否符合OFFICE格式
			return 'pdf';
		} else if (/(.*)\.(xls|XLS|xlsx|XLSX|doc|DOC|docx|DOCX|PPT|PPTX|TXT|ppt|pptx|txt)$/.test(fileNameSuffix)) {
			//根据后缀，判断是否符合OFFICE格式
			return 'office';
		}
		return 'other';
	} catch (error) {
		return 'other';
	}
}

// 后台返回状态码匹配中文
export function getStateFormatZh(list, state) {
	let name = '';
	for (let i = 0; i < list.length; i++) {
		if (list[i].value == state) {
			name = list[i].label;
		}
	}
	return name;
}
